<?php
namespace app\index\controller;
use app\common\model\SystemGoods;
use app\common\service\GoodsService;
use think\Db;

class Goods extends Base{
    public function index()
    {
        $this->goods = SystemGoods::with(['goodsDetail'=>function($query){
            $query->with('service');
        }])->where('status','=',1)->where('show','=',1)
            ->all();
        $this->fetch();
    }

    public function detail()
    {
        if ($this->request->isGet()){
            $parameter = $this->request->get();
            $this->goods = SystemGoods::with(['goodsDetail'=>function($query){
                $query->with('service');
            }])->find($parameter['id']);

            $this->fetch();
        }else{
            $res = GoodsService::buy($this->company['mid'],$this->request->post()['id']);
            if ($res){
                $this->success('购买成功');
            }else{
                $this->error('当前账户点数不足，请联系客服充值');
            }
        }
    }

    public function buy()
    {
        $this->fetch();
    }
}